﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using ViennaAdvantage.Process;
using VAdvantage.Classes;
//using ViennaAdvantage.Model;
using VAdvantage.DataBase;
using VAdvantage.SqlExec;
using System.Data;
using System.Data.SqlClient;
using VAdvantage.Logging;
using VAdvantage.Utility;
using VAdvantage.Report;
//using VAdvantage.Controller;
using VAdvantage.Model;
using VAdvantage.Grids;

namespace VAdvantage.Model
{
    public class CalloutContract : VAdvantage.Model.CalloutEngine
    {
        public String InvoiceCount(Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "" || Util.GetValueOfInt(value) == 0)
            {
                return "";
            }

            DateTime SDate =(DateTime)( mTab.GetValue("StartDate"));
            DateTime Edate = (DateTime)(mTab.GetValue("EndDate"));
            int frequency = Util.GetValueOfInt(mTab.GetValue("C_Frequency_ID"));

            string Sql = "Select NoOfDays from C_Frequency where C_Frequency_ID="+frequency;
            int days =Util.GetValueOfInt( DB.ExecuteScalar(Sql,null,null));
            int totaldays = (Edate-SDate).Days;
            int count = totaldays / days;
            mTab.SetValue("TotalInvoice", count);


            return "";

        }

        public String EndDate(Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            DateTime? SDate =Util.GetValueOfDateTime( mTab.GetValue("StartDate"));
            int frequency = Util.GetValueOfInt(mTab.GetValue("C_Frequency_ID"));
            string Sql = "Select NoOfMonths from C_Frequency where C_Frequency_ID=" + frequency;
            int months = Util.GetValueOfInt(DB.ExecuteScalar(Sql, null, null));
            int invoice=Util.GetValueOfInt(mTab.GetValue("TotalInvoice"));
            DateTime? End = SDate.Value.AddMonths(months * invoice).AddDays(-1);
            mTab.SetValue("EndDate", End);
            if (End < SDate)
            {
                mTab.SetValue("EndDate", SDate);
            }
            return "";
        }

        public String BillStartDate(Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "")
            {
                return "";
            }
            DateTime? SDate = Util.GetValueOfDateTime(mTab.GetValue("StartDate"));
            DateTime? billStartDate = Util.GetValueOfDateTime(mTab.GetValue("BillStartDate"));
            if (billStartDate < SDate)
            {
                ShowMessage.Info("StartDateShouldBeLessThanBillStartDate", true, null, null);
                mTab.SetValue("BillStartDate", mTab.GetValue("StartDate"));
            }
            return "";
        }

        public String StartDate(Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "")
            {
                return "";
            }
            DateTime? SDate = Util.GetValueOfDateTime(mTab.GetValue("StartDate"));
            int frequency = Util.GetValueOfInt(mTab.GetValue("C_Frequency_ID"));
            string Sql = "Select NoOfDays from C_Frequency where C_Frequency_ID=" + frequency;
            int days = Util.GetValueOfInt(DB.ExecuteScalar(Sql, null, null));
            int totalInvoice = Util.GetValueOfInt(mTab.GetValue("TotalInvoice"));
            int cycles = totalInvoice * days;
            DateTime? endDate = Util.GetValueOfDateTime(SDate.Value.AddDays(Util.GetValueOfDouble(cycles)));
            mTab.SetValue("EndDate", endDate);
            
            return "";
        }

        public String StartDateChange(Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "")
            {
                return "";
            }
            int frequency = Util.GetValueOfInt(mTab.GetValue("C_Frequency_ID"));
            string Sql = "Select NoOfMonths from C_Frequency where C_Frequency_ID=" + frequency;
            int months = Util.GetValueOfInt(DB.ExecuteScalar(Sql, null, null));
            int totalInvoice = Util.GetValueOfInt(mTab.GetValue("TotalInvoice"));
            DateTime? SDate = Util.GetValueOfDateTime(value);
            DateTime? endDate = SDate.Value.AddMonths(months * totalInvoice).AddDays(-1);
            mTab.SetValue("EndDate", endDate);
            if (endDate < SDate)
            {
                mTab.SetValue("EndDate", SDate);
            }
            mTab.SetValue("BillStartDate", mTab.GetValue("StartDate"));
            return "";
        }

    }
}
